/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import javax.swing.JTextField;

/**
 *
 * @author Morten J, Morten B & Jonas
 */
public class TextRegler {

    public static void onlyDig(JTextField text) {
	text.setText(text.getText().replaceAll("[^\\d]", ""));
    }

    public static void onlyDouble(JTextField text) {
	text.setText(text.getText().replace(",", "."));
	text.setText(text.getText().replaceAll("[^\\d.]", ""));
	if (text.getText().contains(".")) {
	    int firstDotPosition = text.getText().indexOf(".");
	    String start = text.getText().substring(0, firstDotPosition + 1);
	    String end = text.getText().substring(firstDotPosition);
	    end = end.replace(".", "");
	    if (end.length() > 2) {
		end = end.substring(0, 2);
	    }
	    text.setText(start + end);
	}
    }

    public static double roundDouble(double value) {
	return Math.round(value * 100.0) / 100.0;
    }
}
